<?php
include SYSTEM_ROOTPATH . '/include/get_weather_array.php'; //采集天气
function makeday_static($AreaID, $WeatherCode) {
	//if(getcache($AreaID, 'today')) return false; 		//如果当天采集过了，跳过此文件 
	global $db;
	global $table_top;
	global $tq_tu1;
	global $tq_tu2;
	global $h_dayimg2id;		//白天img转id的数组  其ID对应 $tq_tu1
	global $h_nightimg2id;		//夜晚img转id的数组  其ID对应$tq_tu2
	global $live;
	global $now_table;
	$nowdate = date ( "Y-m-d" );
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "http://www.weather.com.cn/html/weather/$WeatherCode.shtml"); 
	curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
	curl_setopt( $ch, "Connection", "Keep-Alive" );
	curl_setopt( $ch, "User-Agent", "Mozilla/5.0+(compatible;+Googlebot/2.1;++http://www.google.com/bot.html)" );
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
	$Content = curl_exec( $ch );
	curl_close($ch); 
	if ($Content) {

			$up_times = "/周边地区今日天气(.*)发布/isU";
			preg_match_all ( $up_times, $Content, $up_time_array );
			$uptime = $up_time_array ['1'] ['0']; //更新时间
			$uptime = str_replace ( "(", "", $uptime );
			$uptime = str_replace ( "&nbsp;&nbsp;", " ", $uptime );
			$uptime = date ( "Y-m-d", strtotime ( $uptime ) ) . " 08:00:00";
			//if ($up_time == $uptime) {	return false;}
			//开始采集
			$search = array ("'<script[^>]*?>.*?</script>'si", "'<noscript[^>]*?>.*?</noscript>'si", "/<a(.*)>/isU", "/<\/a>/i" ); //去除js和链接
			$replace = array ("", "", "","" );
			$Content = preg_replace ( $search, $replace, $Content );
			$Content = str_replace ( "/m/i/icon_weather/29x20/", "", $Content ); //替换成本地路径
			$Content = str_replace ( "/m/i/indexer/zs/", "", $Content );
			$Content = str_replace ( "/m2/i/icon_weather/29x20/", "", $Content );
			$Content = str_replace ( "/m2/i/indexer/zs/", "", $Content );
			unset ( $up_times, $up_time_array, $up_time, $search, $replace );
			//echo $Content;exit;
			//采集最新实况和相关数据
			$zz = "/<strong style=\"font-size:16px;color:#000;font-weight:bold;\">(.*)<\/strong>/isU";
			preg_match_all ( $zz, $Content, $rc );
			$rc = json_encode ( $rc ['1'] );
			unset ( $zz );
			//采集一周天气
			$zz = "/<td width=\"15%\" rowspan=\"2\" class=\"t0\" style=\"background:#f4f7fc;\">(.*)日(.*)<\/td>.*<img src=\"(.*)\" \/>.*<td width=\"18%\">(.*)<\/td>.*<b>高温 <strong>(.*)℃<\/strong>.*<td width=\"15%\">(.*)<\/td>.*<td width=\"15%\">(.*)<\/td>.*<td style=\"background:#f4f7fc;\">(.*)<\/td>.*<img src=\"(.*)\" \/>.*<td>(.*)<\/td>.*<span>低温 <strong>(.*)℃<\/strong>.*<td>(.*)<\/td>.*<td>(.*)<\/td>/isU";
			preg_match_all ( $zz, $Content, $tq );
		
			unset ( $zz, $tq ['0'],$tq['1'],$tq['2'] );
			
			
			//++++++++++++++++++++++++
			//生成白天天气
			for($j = 0; $j < 31; $j ++) {
				if ($tq_tu1 [$j] ['img'] == $tq ['3'] ['5']) {
					$imgid1 = $j;
					break;
				}
			}
			
			if ($imgid1 == '0') {
				$imgid1a = 0;
				$imgid1b = 2;
			} elseif ($imgid1 == '30') {
				$imgid1a == '28';
				$imgid1b == '30';
			} else {
				$imgid1a = $imgid1 - 1;
				$imgid1b = $imgid1 + 1;
			}
			
			for($i = 6; $i < 31; $i ++) {
				$tq_tu1_id = rand ( $imgid1a, $imgid1b );
				$tq ['3'] [$i] = $tq_tu1 [$tq_tu1_id] ['img'];
				$tq ['4'] [$i] = $tq_tu1 [$tq_tu1_id] ['name'];
			}
			
			//生成夜间天气
			for($j = 0; $j < 31; $j ++) {
				if ($tq_tu1 [$j] ['img'] == $tq ['9'] ['5']) {
					$imgid1 = $j;
					break;
				}
			}
			
			if ($imgid1 == '0') {
				$imgid1a = 0;
				$imgid1b = 2;
			} elseif ($imgid1 == '30') {
				$imgid1a == '28';
				$imgid1b == '30';
			} else {
				$imgid1a = $imgid1 - 1;
				$imgid1b = $imgid1 + 1;
			}
			
			for($i = 6; $i < 31; $i ++) {
				$tq_tu1_id = rand ( $imgid1a, $imgid1b );
				$tq ['9'] [$i] = $tq_tu2 [$tq_tu1_id] ['img'];
				$tq ['10'] [$i] = $tq_tu2 [$tq_tu1_id] ['name'];
			}
			
			//生成白天温度
			$wd1 = $tq ['5'] ['5'];
			$wda = $wd1 - 2;
			$wdb = $wd1 + 2;
			for($i = 6; $i < 31; $i ++) {
				$tq ['5'] [$i] = rand ( $wda, $wdb );
			}
			//生成夜间温度
			$wd2 = $tq ['11'] ['5'];
			$wda = $wd2 - 2;
			$wdb = $wd2 + 2;
			for($i = 6; $i < 31; $i ++) {
				$tq ['11'] [$i] = rand ( $wda, $wdb );
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['11'] [$i] = rand ( $wda, $wdb );
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['6'] [$i] = $tq ['6'] ['5'];
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['7'] [$i] = $tq ['7'] ['5'];
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['12'] [$i] = $tq ['12'] ['5'];
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['13'] [$i] = $tq ['13'] ['5'];
			}
			
			
			//exit ();
			$h_tq = array(
				0=>$h_dayimg2id[$tq[3][0]],	//白天的天气img
				1=>$tq[5][0],	//白天的温度
				2=>$tq[6][0],	//白天的风向
				3=>$tq[7][0],	//白天的风力
				4=>$h_nightimg2id[$tq[9][0]],	//晚上的天气img
				5=>$tq[11][0],	//晚上的温度
				6=>$tq[12][0],	//晚上的风向 
				7=>$tq[13][0],	//晚上的风力 
				);
			
			$tq = json_encode ( $tq );
			$tq = str_replace ( "\\", "@", $tq );
			$tq = str_replace ( " ", "", $tq );
			$tq = str_replace ("@r@n","",$tq);
			$h_tq = json_encode ( $h_tq );
			$h_tq = str_replace ( "\\", "@", $h_tq );
			$h_tq = str_replace ( " ", "", $h_tq );
			$h_tq = str_replace ("@r@n","",$h_tq);

			//采集生活指数
			preg_match_all ( "/<div class=\"todayLiving\" id=\"index\">(.*)<\/div>/isU", $Content, $sh );
			$zz = "/<b class=\".*\">(.*)<\/b>/isU";

			preg_match_all ( $zz, $sh ['1'] ['0'], $sh );
			unset ( $zz, $sh ['0'] );
		
			for($i =0; $i < 20; $i ++)
			{
				$flag = 0;
				for($j =0; $j < count($live[$i]); $j ++)
				{
					if(strcmp(trim($sh['1'][$i]),trim($live[$i][$j])) == 0)
					{
						$sh['1'][$i] = $j;
						$flag = 1;
					}//end if
				}//end for $j
				if($flag == 0)
				{
					$sh['1'][$i] = '1';
				}
			}//end for $i
			$sh = json_encode ( $sh );

		$arr ['xiangguan'] = $rc;
		$arr ['Weather'] = $tq;
		$arr ['live'] = $sh;
		$arr ['WeatherCode'] = $WeatherCode;
		//如果获取这个页面的时间在早晨8点以后，才获取数据，因为页面会在8点钟更新，加这个语句主要是防止8点前有人访问天气页面，这样会自动获取到昨天的数据，造成数据不准
		if(intval(date('H',time())) > 8)
		{
			putfile($AreaID,serialize($arr),24*60,'today');			//存缓存
			if(!$db->getone("select * from {$now_table} where AreaID = '{$AreaID}' and `Update`='{$nowdate}'"))
			{
				$db->insert ( "insert into {$now_table} (`AreaID`,`Weather`,`live`,`xiangguan`,`Update`) values('{$AreaID}','{$h_tq}','{$sh}','{$rc}','{$nowdate}')" );
			}
		}
			return serialize($arr);
		//exit( "insert into {$table_top}weatherlog (`AreaID`,`Weather`,`live`,`xiangguan`,`Update`,`high`,`low`,`hot`,`qihou`) values('{$AreaID}','{$h_tq}','{$sh}','{$rc}','{$nowdate}','{$btwd}','{$yjwd}','0','{$qihou}')" );
	}
}

/**
 * 生成$Area的当天数据
 * @param $Area Area数据库的详细信息
 * @param $type 类型 enum{7,10,15,30,default}
 */

function make_weather($Area, $type = null) {
	global $live;
	global $live_des;
	global $weekarray;
	$citypinyin = $Area ['pinyin'];
	//判断当前城市是否有数据，如果没有，调用父级城市的信息
	
	$Weather = getcache ($Area ['id'],'today' );
	if (! $Weather)
		$Weather = getcache ( $Area ['parentid'],'today' );
	$Weather = unserialize ( $Weather );
	$Weather ['xiangguan'] = str_replace ( "@", "\\", $Weather ['xiangguan'] );
	$Weather ['Weather'] = str_replace ( "@", "\\", $Weather ['Weather'] );
	$Weather ['live'] = str_replace ( "@", "\\", $Weather ['live'] );
	$Weather ['xiangguan'] = json_decode ( $Weather ['xiangguan'], TRUE );
	$Weather ['Weather'] = json_decode ( $Weather ['Weather'], TRUE );
	$Weather ['live'] = json_decode ( $Weather ['live'], TRUE );

	for($i=0; $i < 20; $i ++)
	{
		$idx = intval($Weather['live'][1][$i]); 
		$Weather['live'][1][$i] = $live[$i][$idx];
		$Weather['live'][2][$i] = $live_des[$i][$idx];		
	}
	$WeatherOther = Getone_Weather_Other ( $Area ['id'] ); //同城
	$WeatherOtherCity = Select_Weather_OtherCity ( $Area ['id'] ); //周边
	$month = date ( "m" );
	$day = date ( "d" );
	$WeatherHistory = Getone_History ( $Area ['id'], $month, $day ); //历史气候详情	
	$WeatherShishi_ = json_decode ( getcache($Weather ['WeatherCode'],'realtime' ), true );
	$WeatherShishi = $WeatherShishi_ ['weatherinfo'];
	//分类型生成数据
	switch ($type) {
		case '7' :
			{
				Add_Hot ( $Area ['id'], 'hots' );
				$WeatherHot = Select_Hot ( 'hot', 8 ); //最近关注城市
				$WeatherHots = Select_Hot ( 'hots', 8 ); //最近关注城市
				$WeatherHott = Select_Hot ( 'hott', 8 ); //最近关注城市
				$WeatherHotf = Select_Hot ( 'hotf', 8 ); //最近关注城市
				$WeatherHotm = Select_Hot ( 'hotm', 8 ); //最近关注城市
				$citypinyins = $citypinyin;
				unset ( $citypinyin );
				$title = "{$parentArea['Ab']}{$Area[Ab]}天气预报一周|{$Area[Ab]}天气预报查询一周|{$Area[Ab]}未来一周7天天气预报查询- 天气啦";
				$keywords = "{$Area[Ab]}天气预报一周, {$Area[Ab]}天气预报查询一周,{$Area[Ab]}一周天气预报查询";
				$description = "天气啦(www.tianqi.la)实时提供{$parentArea['Ab']}{$Area[Ab]}气象局播报的{$Area[Ab]}未来天气预报一周,包括提供{$parentArea['Ab']}{$Area[Ab]}地区各区县未来七（7）天的温度,降水量,湿度,风向等超准天气预报查询，让您的旅游出行更加放心!";
				$title2 = "{$Area[Ab]}";
				if ($Area ['parentid'] == '0' && $Area ['property'] == '1') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo7tian/\">全国天气预报一周</a> > <a href=\"/{$Area[pinyin]}7tian.html\">{$Area[Ab]}天气预报一周</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '1' && $Area ['Level'] == '2') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo7tian/\">全国天气预报一周</a> > <a href=\"/{$ppArea[pinyin]}sheng7tian.html\">{$ppArea[name]}天气预报一周</a> > <a href=\"/{$Area[pinyin]}7tian.html\">{$Area[Ab]}天气预报一周</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '2' && $Area ['Level'] == '2') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo7tian/\">全国天气预报一周</a> > <a href=\"/{$parentArea[pinyin]}7tian.html\">{$parentArea[name]}天气预报一周</a> > <a href=\"/{$Area[pinyin]}7tian.html\">{$Area[Ab]}天气预报一周</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '2' && $Area ['Level'] == '3') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo7tian/\">全国天气预报一周</a> > <a href=\"/{$ppArea[pinyin]}sheng7tian.html\">{$ppArea[name]}天气预报一周</a> > <a href=\"/{$parentArea[pinyin]}7tian.html\">{$parentArea[Ab]}天气预报一周</a> > <a href=\"/{$Area[pinyin]}7tian.html\">{$Area[Ab]}天气预报一周</a>";
				} else {
					$daohang = "<a href=\"/\">天气预报网</a> > <a href=\"/quanguo7tian/\">全国天气预报一周</a> > <a href=\"/{$ppArea[pinyin]}sheng7tian.html\">{$ppArea[name]}天气预报一周</a> > <a href=\"/{$parentArea[pinyin]}7tian.html\">{$parentArea[Ab]}天气预报一周</a> > <a href=\"/{$Area[pinyin]}7tian.html\">{$Area[Ab]}天气预报一周</a>";
				}
				ob_start ();
				include template ( 'weathers' ); //一周天气预报模版
				///缓存文件保存到缓存目录中
				$file = ob_get_contents ();
				ob_end_clean ();
				putfile($Area['pinyin']."_7",$file,30,"pages");
				return $file;
				break;
			}
		case '10' :
			{
				Add_Hot ( $Area ['id'], 'hott' );
				$WeatherHot = Select_Hot ( 'hot', 8 ); //最近关注城市
				$WeatherHots = Select_Hot ( 'hots', 8 ); //最近关注城市
				$WeatherHott = Select_Hot ( 'hott', 10 ); //最近关注城市
				$WeatherHotf = Select_Hot ( 'hotf', 8 ); //最近关注城市
				$WeatherHotm = Select_Hot ( 'hotm', 8 ); //最近关注城市
				$citypinyint = $citypinyin;
				unset ( $citypinyin );
				$title = "{$parentArea['Ab']}{$Area[Ab]}天气预报10天|{$Area[Ab]}天气预报10天查询|{$Area[Ab]}未来10天(十天)天气预报查询-天气啦";
				$keywords = "{$Area[Ab]}天气预报10天,{$Area[Ab]}天气预报10天查询,{$Area[Ab]}天气预报未来10天";
				$description = "天气啦(www.tianqi.la)实时提供{$parentArea['Ab']}{$Area[Ab]}气象局播报的{$Area[Ab]}未来天气预报10天,包括提供{$parentArea['Ab']}{$Area[Ab]}地区各区县未来10天(十天)的温度,降水量,湿度,风向等超准天气预报查询，让您的旅游出行更加放心!";
				$title2 = "{$Area[Ab]}";
				if ($Area ['parentid'] == '0' && $Area ['property'] == '1') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo10tian/\">全国天气预报10天</a> > <a href=\"/{$Area[pinyin]}10tian.html\">{$Area[Ab]}天气预报10天</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '1' && $Area ['Level'] == '2') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo10tian/\">全国天气预报10天</a> > <a href=\"/{$ppArea[pinyin]}sheng10tian.html\">{$ppArea[name]}天气预报10天</a> > <a href=\"/{$Area[pinyin]}10tian.html\">{$Area[Ab]}天气预报10天</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '2' && $Area ['Level'] == '2') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo10tian/\">全国天气预报10天</a> > <a href=\"/{$parentArea[pinyin]}10tian.html\">{$parentArea[name]}天气预报10天</a> > <a href=\"/{$Area[pinyin]}10tian.html\">{$Area[Ab]}天气预报10天</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '2' && $Area ['Level'] == '3') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo10tian/\">全国天气预报10天</a> > <a href=\"/{$ppArea[pinyin]}sheng10tian.html\">{$ppArea[name]}天气预报10天</a> > <a href=\"/{$parentArea[pinyin]}10tian.html\">{$parentArea[Ab]}天气预报10天</a> > <a href=\"/{$Area[pinyin]}10tian.html\">{$Area[Ab]}天气预报10天</a>";
				} else {
					$daohang = "<a href=\"/\">天气预报网</a> > <a href=\"/quanguo10tian/\">全国天气预报10天</a> > <a href=\"/{$ppArea[pinyin]}sheng10tian.html\">{$ppArea[name]}天气预报10天</a> > <a href=\"/{$parentArea[pinyin]}10tian.html\">{$parentArea[Ab]}天气预报10天</a> > <a href=\"/{$Area[pinyin]}10tian.html\">{$Area[Ab]}天气预报10天</a>";
				}
				///缓存文件保存到缓存目录中
				ob_start ();
				include template ( 'weathert' ); //十天天气预报模版				
				$file = ob_get_contents ();
				ob_end_clean ();
				putfile($Area['pinyin']."_10",$file,30,"pages");
				return $file;
				break;
			}
		case '15' :
			{
				Add_Hot ( $Area ['id'], 'hotf' );
				$WeatherHot = Select_Hot ( 'hot', 8 ); //最近关注城市
				$WeatherHots = Select_Hot ( 'hots', 8 ); //最近关注城市
				$WeatherHott = Select_Hot ( 'hott', 8 ); //最近关注城市
				$WeatherHotf = Select_Hot ( 'hotf', 8 ); //最近关注城市
				$WeatherHotm = Select_Hot ( 'hotm', 8 ); //最近关注城市
				$citypinyinf = $citypinyin;
				unset ( $citypinyin );
				$title = "{$parentArea['Ab']}{$Area[Ab]}天气预报15天|{$Area[Ab]}天气预报15天查询|{$Area[Ab]}未来15天天气预报查询-天气啦";
				$keywords = "{$Area[Ab]}天气预报15天,{$Area[Ab]}天气预报15天查询,{$Area[Ab]}天气预报未来15天";
				$description = "天气啦(www.tianqi.la)实时提供{$parentArea['Ab']}{$Area[Ab]}气象局播报的{$Area[Ab]}未来天气预报15天,包括提供{$parentArea['Ab']}{$Area[Ab]}地区各区县未来15天的温度,降水量,湿度,风向等超准天气预报查询，让您的旅游出行更加放心!"; //十五天气预报模版
				$title2 = "{$Area[Ab]}";
				if ($Area ['parentid'] == '0' && $Area ['property'] == '1') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo15tian/\">全国天气预报15天</a> > <a href=\"/{$Area[pinyin]}15tian.html\">{$Area[Ab]}天气预报15天</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '1' && $Area ['Level'] == '2') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo15tian/\">全国天气预报15天</a> > <a href=\"/{$ppArea[pinyin]}sheng15tian.html\">{$ppArea[name]}天气预报15天</a> > <a href=\"/{$Area[pinyin]}15tian.html\">{$Area[Ab]}天气预报15天</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '2' && $Area ['Level'] == '2') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo15tian/\">全国天气预报15天</a> > <a href=\"/{$parentArea[pinyin]}15tian.html\">{$parentArea[name]}天气预报15天</a> > <a href=\"/{$Area[pinyin]}15tian.html\">{$Area[Ab]}天气预报15天</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '2' && $Area ['Level'] == '3') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo15tian/\">全国天气预报15天</a> > <a href=\"/{$ppArea[pinyin]}sheng15tian.html\">{$ppArea[name]}天气预报15天</a> > <a href=\"/{$parentArea[pinyin]}15tian.html\">{$parentArea[Ab]}天气预报15天</a> > <a href=\"/{$Area[pinyin]}15tian.html\">{$Area[Ab]}天气预报15天</a>";
				} else {
					$daohang = "<a href=\"/\">天气预报网</a> > <a href=\"/quanguo15tian/\">全国天气预报15天</a> > <a href=\"/{$ppArea[pinyin]}sheng15tian.html\">{$ppArea[name]}天气预报15天</a> > <a href=\"/{$parentArea[pinyin]}15tian.html\">{$parentArea[Ab]}天气预报15天</a> > <a href=\"/{$Area[pinyin]}15tian.html\">{$Area[Ab]}天气预报15天</a>";
				}
				///缓存文件保存到缓存目录中
				ob_start ();
				include template ( 'weatherf' ); //十五天气预报模版
				$file = ob_get_contents ();
				ob_end_clean ();
				putfile($Area['pinyin']."_15",$file,30,"pages");
				return $file;
				break;
			}
		case '30' :
			{
				$WeatherHot = Select_Hot ( 'hot', 20 ); //最近关注城市
				$WeatherHots = Select_Hot ( 'hots', 20 ); //最近关注城市
				$WeatherHott = Select_Hot ( 'hott', 20 ); //最近关注城市
				$WeatherHotf = Select_Hot ( 'hotf', 20 ); //最近关注城市
				$WeatherHotm = Select_Hot ( 'hotm', 20 ); //最近关注城市
				$WeatherHot_ = $WeatherHot;
				Add_Hot ( $Area ['id'], 'hotm' );
				$citypinyinm = $citypinyin;
				unset ( $citypinyin );
				$title = "{$parentArea['Ab']}{$Area[Ab]}天气预报30天|{$Area[Ab]}天气预报30天查询|{$Area[Ab]}未来30天(一个月)天气预报查询 -天气啦";
				$keywords = "{$Area[Ab]}天气预报30天,{$Area[Ab]}天气预报一个月,{$Area[Ab]}天气预报未来30天(一个月)查询";
				$description = "天气啦(www.tianqi.la)实时提供{$parentArea['Ab']}{$Area[Ab]}气象局播报的{$Area[Ab]}未来天气预报30天,包括提供{$parentArea['Ab']}{$Area[Ab]}地区各区县未来一个月30天的温度,降水量,湿度,风向等超准天气预报查询，让您的旅游出行更加放心!";
				$title2 = "{$Area[Ab]}";
				if ($Area ['parentid'] == '0' && $Area ['property'] == '1') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo30tian/\">全国天气预报30天</a> > <a href=\"/{$Area[pinyin]}30tian.html\">{$Area[Ab]}天气预报30天</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '1' && $Area ['Level'] == '2') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo30tian/\">全国天气预报30天</a> > <a href=\"/{$ppArea[pinyin]}sheng30tian.html\">{$ppArea[name]}天气预报30天</a> > <a href=\"/{$Area[pinyin]}30tian.html\">{$Area[Ab]}天气预报30天</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '2' && $Area ['Level'] == '2') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo30tian/\">全国天气预报30天</a> > <a href=\"/{$parentArea[pinyin]}30tian.html\">{$parentArea[name]}天气预报30天</a> > <a href=\"/{$Area[pinyin]}30tian.html\">{$Area[Ab]}天气预报30天</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '2' && $Area ['Level'] == '3') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo30tian/\">全国天气预报30天</a> > <a href=\"/{$ppArea[pinyin]}sheng30tian.html\">{$ppArea[name]}天气预报30天</a> > <a href=\"/{$parentArea[pinyin]}30tian.html\">{$parentArea[Ab]}天气预报30天</a> > <a href=\"/{$Area[pinyin]}30tian.html\">{$Area[Ab]}天气预报30天</a>";
				} else {
					$daohang = "<a href=\"/\">天气预报网</a> > <a href=\"/quanguo30tian/\">全国天气预报30天</a> > <a href=\"/{$ppArea[pinyin]}sheng30tian.html\">{$ppArea[name]}天气预报30天</a> > <a href=\"/{$parentArea[pinyin]}30tian.html\">{$parentArea[Ab]}天气预报30天</a> > <a href=\"/{$Area[pinyin]}30tian.html\">{$Area[Ab]}天气预报30天</a>";
				}
				///缓存文件保存到缓存目录中
				ob_start ();
				include template ( 'weatherm' ); //三十天气预报模版
				$file = ob_get_contents ();
				ob_end_clean ();
				putfile($Area['pinyin']."_30",$file,30,"pages");
				return $file;
				break;
			}
		default :
			{
				
				$WeatherHot = Select_Hot ( 'hot', 12 ); //最近关注城市
				$WeatherHots = Select_Hot ( 'hots', 8 ); //最近关注城市
				$WeatherHott = Select_Hot ( 'hott', 10 ); //最近关注城市
				$WeatherHotf = Select_Hot ( 'hotf', 10 ); //最近关注城市
				$WeatherHotm = Select_Hot ( 'hotm', 10 ); //最近关注城市
				$WeatherHot_ = $WeatherHot;
				Add_Hot ( $Area ['id'], 'hot' );
				$title = "{$parentArea['Ab']}{$Area[Ab]}天气预报|{$Area[Ab]}天气预报查询|{$Area[Ab]}未来三天(今天,明天,后天)天气预报查询-天气啦";
				$keywords = "{$Area[Ab]}天气预报,{$Area[Ab]}天气预报查询";
				$description = "天气啦(www.tianqi.la)实时提供{$parentArea['Ab']}{$Area[Ab]}气象局播报的{$Area[Ab]}天气预报今天,明天,后天未来三天天气预报,包括提供{$parentArea['Ab']}{$Area[Ab]}地区各区县未来3天的温度,降水量,湿度,风向等超准天气预报查询，让您的旅游出行更加放心!";
				$title2 = "{$Area[Ab]}";
				if ($Area ['parentid'] == '0' && $Area ['property'] == '1') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo/\">全国天气预报</a> > <a href=\"/{$Area[pinyin]}.html\">{$Area[Ab]}天气预报</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '1' && $Area ['Level'] == '2') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo/\">全国天气预报</a> > <a href=\"/{$ppArea[pinyin]}sheng.html\">{$ppArea[name]}天气预报</a> > <a href=\"/{$Area[pinyin]}.html\">{$Area[Ab]}天气预报</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '2' && $Area ['Level'] == '2') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo/\">全国天气预报</a> > <a href=\"/{$parentArea[pinyin]}.html\">{$parentArea[name]}天气预报</a> > <a href=\"/{$Area[pinyin]}.html\">{$Area[Ab]}天气预报</a>";
				} elseif ($Area ['parentid'] !== '0' && $Area ['property'] == '2' && $Area ['Level'] == '3') {
					$daohang = "<a href=\"/\">天气预报查询网</a> > <a href=\"/quanguo/\">全国天气预报</a> > <a href=\"/{$ppArea[pinyin]}sheng.html\">{$ppArea[name]}天气预报</a> > <a href=\"/{$parentArea[pinyin]}.html\">{$parentArea[Ab]}天气预报</a> > <a href=\"/{$Area[pinyin]}.html\">{$Area[Ab]}天气预报</a>";
				} else {
					$daohang = "<a href=\"/\">天气预报网</a> > <a href=\"/quanguo/\">全国天气预报</a> > <a href=\"/{$ppArea[pinyin]}sheng.html\">{$ppArea[name]}天气预报</a> > <a href=\"/{$parentArea[pinyin]}.html\">{$parentArea[Ab]}天气预报</a> > <a href=\"/{$Area[pinyin]}.html\">{$Area[Ab]}天气预报</a>";
				}
				if (! empty ( $_GET ['date'] )) {
					$Weather ['Weather'] [4] [0] != $Weather ['Weather'] [10] [0] && $tadd = "转{$Weather['Weather'][10][0]}";
					$title = $Area ['Ab'] . substr ( $_GET ['date'], 0, 4 ) . "年" . substr ( $_GET ['date'], 4, 2 ) . "月" . substr ( $_GET ['date'], 6, 2 ) . "日天气预报(" . $Weather ['Weather'] [4] [0] . $tadd . ",最高温度" . $Weather ['Weather'] [5] [0] . "℃,最低温度" . $Weather ['Weather'] [11] [0] . "℃)";
				}
				define ( 'DEDEINC', ereg_replace ( "[/\\]{1,}", '/', dirname ( __FILE__ ) ) );
				define ( 'DEDEROOT', ereg_replace ( "[/\\]{1,}", '/', substr ( DEDEINC, 0, - 8 ) ) );
				define ( 'DEDEDATA', DEDEROOT . '/data' );
				define ( 'DEDEMEMBER', DEDEROOT . '/member' );
				define ( 'DEDETEMPLATE', DEDEROOT . '/templets' );
				include_once DEDEINC . '/common.inc.php';
				include_once DEDEINC . "/arc.partview.class.php";
$tem=<<<EOT

<ul>

{dede:arclist  row=5 titlelen=48 orderby='pubdate' }

<li style="list-style-type:disc;"><a href="[field:typeurl/]">[[field:typename/]]</a>&nbsp;&nbsp;<a href="[field:arcurl/]">[field:title/]</a></li>

{/dede:arclist}

</ul>

EOT;
				
				$pv = new PartView ();
				$pv->SetTemplet ( $tem, "string" );
				$dedeall = $pv->GetResult ();
				$cityid && $WeatherHot = $WeatherHot_;
				ob_start ();
				include template ( 'weather' ); //今日天气预报模版
				///缓存文件保存到缓存目录中
				$file = ob_get_contents ();
				ob_end_clean ();
				putfile($Area['pinyin'],$file,30,"pages");
				return $file;
				break ;
			}
	}
}

//缓存时时天气
function makeWeatherShishi($WeatherCode) {
	//if(time() - filemtime(h_cache_half_clock . $WeatherCode) < 30*60) {return false;} //如果采集的时间小于半小时，则跳过
	$content = geturl ( "http://www.weather.com.cn/data/sk/" . $WeatherCode . ".html" );
	putfile($WeatherCode, $content, 30, 'realtime');
	return $content;
}

//获取URL
function geturl($url) {
	$ch = curl_init ();
	curl_setopt ( $ch, CURLOPT_URL, $url );
	curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt ( $ch, CURLOPT_HEADER, 0 );
	$cnt = curl_exec ( $ch );
	curl_close ( $ch );
	return $cnt;
}

function GetQiHou1($qihou) {
	if (empty ( $qihou )) {
		return false;
	}
	if (strstr ( $qihou, '晴' )) {
		$qihouid = '1';
	} elseif (strstr ( $qihou, '阴' )) {
		$qihouid = '2';
	} elseif (strstr ( $qihou, '雨' )) {
		$qihouid = '3';
	} elseif (strstr ( $qihou, '雪' )) {
		$qihouid = '4';
	} elseif ($qihou == '多云') {
		$qihouid = '5';
	}
	return $qihouid;
}
/**
 * 生成文件 
 * @param $filename 文件名 
 * @param $cnt 内容 
 * @param $time 对于mem来说的缓存时间
 * @param $filetype cache_cfg的缓存类型
 */
function putfile($filename,$cnt,$time,$filetype)
{
	global $CACHE_TYPE;
	global $cache_cfg;
	switch($CACHE_TYPE)
	{
	case 1:
		{
		
			global $mem;
				$mem->set($cache_cfg[$filetype].$filename,$cnt,MEMCACHE_COMPRESSED,$time);
				break;
		}
	
	case 2:
		{
			file_put_contents($cache_cfg[$filetype].$filename.'.html',$cnt);
			break;
		}
	}
}



/**
 * 判断内容是否存在，如果已过期或者不存在，则返回false,如果存在，则返回
 * 主要用在定时采集的程序中。
 */
function hasfile($filename,$cache_type,$day=null)
{
	global $CACHE_TYPE;
	global $cache_cfg;
	switch($CACHE_TYPE)
	{
		case 1:	//缓存类型为内存
			{
				global $mem;		//memcache缓存类 在 common.php 中定义
				switch ($cache_type)
				{
					case 'pages':
						{
							$citypinyin_f = $filename;
							if($day) $citypinyin_f.="_".$day;
							if($result = $mem->get($cache_cfg[$cache_type].$citypinyin_f))
							{
								return $result;
							}
							else
							{
								return false;
							}
							break;
						}
					case 'realtime':
						{
							if($result = $mem->get($cache_cfg[$cache_type].$filename))
							{
								return $result;
							}
							else
							{
								return false;
						
							}
							break;
						}
						
					case 'today':
						{
							if($result = $mem->get($cache_cfg[$cache_type].$filename))
							{
								return $result;
							}
							else
							{
								return false;
								
							}
							break;
						}
				}
				break;
			}
		case 2:
			{
				switch ($cache_type)
				{
					case 'pages':
						{
							$citypinyin_f = $filename;
							if($day) $citypinyin_f.="_".$day;
							if(((time() - filemtime($cache_cfg[$cache_type].$citypinyin_f.'.html') < 30*60)) && ($result = file_get_contents($cache_cfg['pages'].$citypinyin_f.'.html')))							
							{
								return $result;
							}
							else
							{
								return false;
							}
							break;
						}
					case 'realtime':
						{
							if(((time() - filemtime($cache_cfg[$cache_type].$filename.'.html')) < 30*60) && ($result = file_get_contents($cache_cfg[$cache_type].$filename.'.html')))
							{
								return $result;
							}
							else
							{
								return false;
							}
							break;
						}
					case 'today':
						{
							if((date('Y-m-d',filemtime($cache_cfg[$cache_type].$filename.'.html')) == date('Y-m-d',time())) && ($result = file_get_contents($cache_cfg[$cache_type].$filename.'.html')) )
							{
								return $result;
							}
							else
							{
								return false;
							}
							break;
						}
				}
				break;
			}
	}
}
/**
 * 获取缓存的内容
 * @param $filename 文件名(pages = pinyin , today = areaid, realtime = weathercode 
 * @param $cache_type 缓存类型  pages/realtime/today
 * @param $day cache_type = pages 调用哪天的缓存 
 */
function getcache($filename,$cache_type,$day=null)
{
	if($result = hasfile($filename,$cache_type,$day))
	{
		return $result;
	}
	else
	{
		global $db;
		global $table_top;
		switch ($cache_type)
		{
			case 'pages':
				{
					$Area = Getone_Area_Pinyin($filename);
					return make_weather($Area,$day);
					break;
				}
			case 'realtime':
				{
					return makeWeatherShishi($filename);
					break;	
				}
			case 'today':
				{
					
					$sql = $db->getone("select AreaID,WeatherCode from {$table_top}weather where AreaID ='{$filename}' limit 0,1");
					if(!$sql) return false;		//如果不存在这个地区的数据，返回false
					return makeday_static($sql['AreaID'],$sql['WeatherCode']);
				}
		}
	}
}
/**
 * 删除缓存文件
 * @param $filename 文件名(pages = pinyin , today = areaid, realtime = weathercode 
 * @param $cache_type 缓存类型  pages/realtime/today
 * @param $day cache_type = pages 调用哪天的缓存 
 */ 
function delcache($filename,$cache_type,$day=null)
{
	global $CACHE_TYPE;
	global $cache_cfg;
	if($cache_type =='pages' && $day != null) $filename.="_".$day;
	switch($CACHE_TYPE)
	{
		case 1:
			{
				global $mem;
				$mem->delete($cache_cfg[$cache_type].$filename);
				break;
			}
		case 2:
			{
				unlink($cache_cfg[$cache_type].$filename.".html");
			}
	}
}